package ylg.cn.com.expandlistview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/8/25.
 */

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    public String[] group = { "常用", "搞笑", "原创", "资讯", "体育", "游戏", "汽车", "娱乐" };

    public String[][] gridViewChild = {
            { "明星八卦", "雷人囧事", "网络红人", "科技资讯", "美女花边", "游戏达人", "美女车模", "疯狂恶搞" },
            { "疯狂恶搞", "搞笑综艺", "原创搞笑", "爆笑宠物", "雷人囧事" },
            { "原创热点", "原创影视", "音乐动画", "火星搞笑", "校园作品", "网络红人", "拍客" },
            { "社会资讯", "国内资讯", "国际资讯", "财富资讯", "科技资讯" },
            { "篮球天地", "足球世界", "综合体育", "极限运动", "武术摔角", "美女花边" },
            { "网络游戏", "电子竞技", "单机电玩", "游戏达人", "工会战队" },
            { "新车速递", "车型推荐", "改装酷玩", "汽车广告", "评测报告", "美女车模" },
            { "明星八卦", "影视资讯" } };
    String[][] child = { { "" }, { "" }, { "" }, { "" }, { "" }, { "" },
            { "" }, { "" } };
    LayoutInflater mInflater;
    Context context;

    public ExpandableListViewAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return group.length;
    }

    @Override
    public int getChildrenCount(int i) {
        return child[i].length;
    }

    @Override
    public Object getGroup(int i) {
        return group[i];
    }

    @Override
    public Object getChild(int i, int i1) {
        return child[i][i1];
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewChild mViewChild;
        if (view==null){
            mViewChild = new ViewChild();
            view = mInflater.inflate(
                    R.layout.channel_expandablelistview, null);
            mViewChild.textView = (TextView) view
                    .findViewById(R.id.channel_group_name);
            mViewChild.imageView = (ImageView) view
                    .findViewById(R.id.channel_imageview_orientation);
            view.setTag(mViewChild);
        }else{
            mViewChild = (ViewChild) view.getTag();
        }
        if (b) {
            mViewChild.imageView
                    .setImageResource(R.drawable.channel_expandablelistview_top_icon);
        } else {
            mViewChild.imageView
                    .setImageResource(R.drawable.channel_expandablelistview_bottom_icon);
        }
        mViewChild.textView.setText(getGroup(i).toString());
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewChild viewChild;
        if (view==null){
            viewChild = new ViewChild();
            view = mInflater.inflate(R.layout.channel_expandablelistview_item,null);
            viewChild.gridView = (GridView) view
                    .findViewById(R.id.channel_item_child_gridView);
            view.setTag(viewChild);
        }else{
            viewChild = (ViewChild) view.getTag();
        }
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(context,
                setGridViewData(gridViewChild[i]),
                R.layout.channel_gridview_item,
                new String[] { "channel_gridview_item" },
                new int[] { R.id.channel_gridview_item });
        viewChild.gridView.setAdapter(mSimpleAdapter);
        setGridViewListener( viewChild .gridView);
        viewChild .gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        return view;
    }
    /**
     * 设置gridview数据
     *
     * @param data
     * @return
     */
    private ArrayList<HashMap<String, Object>> setGridViewData(String[] data) {
        ArrayList<HashMap<String, Object>> gridItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < data.length; i++) {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("channel_gridview_item", data[i]);
            gridItem.add(hashMap);
        }
        return gridItem;
    }
    /**
     * 设置gridview点击事件监听
     *
     * @param gridView
     */
    private void setGridViewListener(final GridView gridView) {
        gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                if (view instanceof TextView) {
                    // 如果想要获取到哪一行，则自定义gridview的adapter，item设置2个textview一个隐藏设置id，显示哪一行
                    TextView tv = (TextView) view;
                    Toast.makeText(context,
                            "position=" + position + "||" + tv.getText(),
                            Toast.LENGTH_SHORT).show();
                    Log.e("hefeng", "gridView listaner position=" + position
                            + "||text=" + tv.getText());
                }
            }
        });
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    static class ViewChild {
        ImageView imageView;
        TextView textView;
        GridView gridView;
    }

}
